﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace _queensGA
{
    public partial class ChessBoard : UserControl
    {
        private int boardLength = 8;
        private int tileSize = 10;
        private Font fntDefault = new Font("Sans Serif", 18.0f);
        private bool done = false;
        private List<int> state = new List<int>();

        public ChessBoard()
        {
            InitializeComponent();
            ResizeRedraw = true;
        }

        public void SetupBoard(int boardLength, int tileSize)
        {
            this.boardLength = boardLength;
            this.tileSize = tileSize;

            this.Width = boardLength * tileSize + 1;
            this.Height = this.Width;
            picBoard.BackColor = Color.Azure;
        }

        public void SetState(List<int> state, bool isDone)
        {
            done = isDone;
            this.state = state;
        }

        private void DrawState(Graphics g)
        {
            if (boardLength != state.Count)
                return;

            for (int i = 0; i < boardLength; ++i)
            {
                g.DrawString("o", fntDefault, done ? Brushes.Blue : Brushes.Black, state[i] * tileSize, i * tileSize);
            }
        }

        private void picBoard_Paint(object sender, PaintEventArgs e)
        {
            Brush b1 = Brushes.Azure;
            Brush b2 = Brushes.Aquamarine;
            bool first = false;
            bool sec = false;

            for (int i = 0; i <= boardLength; i++)
            {
                first = !first;
                sec = first;
                for (int j = 0; j <= boardLength; j++)
                {
                    sec = !sec;
                    if (sec)
                        e.Graphics.FillRectangle(b1, i * tileSize + 1, j * tileSize + 1, (i + 1) * tileSize + 1, (j + 1) * tileSize + 1);
                    else
                        e.Graphics.FillRectangle(b2, i * tileSize + 1, j * tileSize + 1, (i + 1) * tileSize + 1, (j + 1) * tileSize + 1);
                }
            }

            for (int i = 0; i <= boardLength; ++i)
            {
                e.Graphics.DrawLine(Pens.Black, i * tileSize, 0, i * tileSize, tileSize * boardLength);
                e.Graphics.DrawLine(Pens.Black, 0, i * tileSize, tileSize * boardLength, i * tileSize);
            }

            DrawState(e.Graphics);
        }
    }
}
